import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DatePipe } from '@angular/common';
import { Router } from '@angular/router';
import { SellLuckdrawManageService } from '../sell-luckdraw-manage.service';
import { CommonService } from '../../../../services-common/common.service'

@Component({
  selector: 'app-order-lottery-record',
  templateUrl: './order-lottery-record.component.html',
  styleUrls: ['./order-lottery-record.component.scss'],
  providers: [DatePipe]
})
export class OrderLotteryRecordComponent implements OnInit {
  isCollapse: boolean = false;

  // 配置下拉list
  selectParams: any = {
    loading: false,
    configOptions: [],
    configSelected: [],
    page: 1,
    pageSize: 15,
    total: 0
  };

  seachParams: any = {
    dayBeginTime: '', // 开始时间
    dayEndTime: '', // 结束时间
    couponNo: '', // 奖品券码
    orderNum: '', // 订单编号
  };
  backStatusArr: any = []; // 状态
  prizeDates: any = []; // 日期

  tableData: any = [];
  tableParams: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0,
  };

  // export
  exportLoading: boolean = false;

  constructor(
    private services: SellLuckdrawManageService,
    private message: NzMessageService,
    private commonService: CommonService,
    private datePipe: DatePipe,
    private router: Router
  ) { }

  ngOnInit(): void {
    this.getConfigureList();
    this.loadTable();
  }

  // 获取抽奖配置列表
  async getConfigureList() {
    try {
      let params = {
        page: this.selectParams.page,
        pageSize: this.selectParams.pageSize
      }
      this.selectParams.loading = true;
      const res: any = await this.services.queryLotteryList(params);
      this.selectParams.loading = false;
      
      if (res.code == 0) {
        const { records, total } = res.data || {};
        this.selectParams.configOptions = this.selectParams.configOptions.concat(records || []);
        this.selectParams.total = total || 0;
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.selectParams.loading = false;
      this.selectParams.configOptions = [];
      this.selectParams.total = 0;
      this.message.error(error);
    }
  }
  loadMore() {
    if (this.selectParams.total == this.selectParams.configOptions.length) {
      return
    }
    this.selectParams.page += 1;
    this.getConfigureList();
  }

  async loadTable() {
    try {
      let params = {
        ...this.seachParams,
        activityIdList: this.selectParams.configSelected.join(','), // 抽奖配置
        backStatus: this.backStatusArr.join(','), // 状态
        page: this.tableParams.page,
        pageSize: this.tableParams.pageSize
      }
      this.tableParams.tableLoading = true;
      const res: any = await this.services.querylotteryRecord(params);
      this.tableParams.tableLoading = false;
      
      if (res.code == 0) {
        const { records, total } = res.data || {};
        this.tableData = records || [];
        this.tableParams.total = total || 0;
      } else {
        this.message.error(res.message);
      }
    } catch (error: any) {
      this.tableParams.tableLoading = false;
      this.message.error(error);
    }
  }

  seachData() {
    this.tableParams.page = 1;
    this.loadTable();
  }
  resetData() {
    for (let key in this.seachParams) {
      this.seachParams[key] = '';
    }
    this.selectParams.configSelected = []; // 已选配置
    this.backStatusArr = []; // 已选状态
    this.prizeDates = []; // 日期数组

    this.tableParams.page = 1;
    this.loadTable();
  }

  // date cahnge
  onDateChange(result: Date[]) {
    this.seachParams.dayBeginTime = result.length ? this.datePipe.transform(result[0], 'yyyy-MM-dd') : '';
    this.seachParams.dayEndTime = result.length ? this.datePipe.transform(result[1], 'yyyy-MM-dd') : '';
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // 页数改变时的回调函数
  onPageSizeChange(sizeNum: number) {
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // export
  async exportTable() {
    let params = {
      ...this.seachParams,
      activityIdList: this.selectParams.configSelected.join(','), // 抽奖配置
      backStatus: this.backStatusArr.join(','), // 状态
      page: 1,
      pageSize: 100000
    }
    this.exportLoading = true;
    const res: any = await this.services.exportActivityRecordData(params);
    this.exportLoading = false;

    if (res.type == 'application/octet-stream') {
      this.commonService.downLoadBlobFile(res, '下单抽奖记录');
    } else {
      this.message.error('导出失败');
    }
  }
}
